package com.smart.stock.module.maindata.controller;

import com.alibaba.fastjson.JSON;
import com.mongodb.bulk.BulkWriteResult;
import com.smart.stock.common.db.MyMongoTemplate;
import com.smart.stock.module.maindata.entity.CsIndexIndustry;
import com.smart.stock.common.util.stock.StockSpider;
import com.smart.stock.module.maindata.entity.StockPriceToday;
import com.smart.stock.module.maindata.services.*;
import com.smart.stock.module.spider.jobSpiderWeb.csindex.CsindexIndustrySpider;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.concurrent.FutureTask;

/**
 * @author chenguoxiang
 * @create 2018-03-27 11:14
 **/
@Api(tags = "API中心:用于手动刷新接口数据")
@RequestMapping("/stock")
@Slf4j
@RestController
public class RefreshDataController {

    @Autowired
    private StockService service;
    @Autowired
    private StockCodeInfoService stockCodeInfoService;
    @Autowired
    private StockCompanyService stockCompanyService;
    @Autowired
    private StockDailyPriceServices stockDailyPriceServices;
    @Autowired
    private StockDailyIndexesService stockDailyIndexesService;
    @Autowired
    private StockSpider spider;
    @Autowired
    private BoardIndustryService boardIndustryService;
    @Autowired
    private StockPriceTodayService stockPriceTodayService;

    @ApiOperation(value = "刷新代码", notes = "初始化所有代码,tushare接口到stock_code_info")
    @ResponseBody
    @PostMapping(value = "/refresh_code")
    public String initCode() {
        try {
            stockCodeInfoService.refreshCode();
            return "success";
        } catch (Exception e) {
            e.printStackTrace();
            return e.getMessage();
        }
    }

    @ApiOperation(value = "刷新公司", notes = "初始化所有公司信息,tushare接口到stock_company")
    @ResponseBody
    @PostMapping(value = "/refresh_company")
    public String initCompany() {
        try {
            stockCompanyService.refreshStockCompany();
            return "success";
        } catch (Exception e) {
            e.printStackTrace();
            return e.getMessage();
        }
    }
    @ApiOperation(value = "初始化所有股票的历史价格", notes = "初始化所有股票的历史价格,tushare接口到stock_price_daily")
    @ResponseBody
    @PostMapping(value = "/initDailyPrice")
    public String initDailyPrice() {
        try {
            stockDailyPriceServices.initDailyPrice();
            return "success";
        } catch (Exception e) {
            e.printStackTrace();
            return e.getMessage();
        }
    }
    @ApiOperation(value = "刷新单个股票的历史价格", notes = "刷新单个股票的历史价格,tushare接口到stock_price_dail")
    @ResponseBody
    @PostMapping(value = "/refreshDailyPrice")
    public String refreshDailyPrice(@RequestParam String code,@RequestParam String startDate,@RequestParam String end) {
        try {
            stockDailyPriceServices.refreshDailyPrice(code,startDate,end);
            return "success";
        } catch (Exception e) {
            e.printStackTrace();
            return e.getMessage();
        }
    }

    @ApiOperation(value = "刷新多个股票的价格到今天，英文逗号隔开", notes = "刷新单个股票的价格到今天,tushare接口到stock_price_dail")
    @ResponseBody
    @PostMapping(value = "/refreshCodeListDailyPriceToMax")
    public String refreshCodeListDailyPriceToMax(@RequestParam String codes) {
        try {
            String[] split = codes.split(",");
            stockDailyPriceServices.refreshCodeListDailyPriceToMax(split);
            return "success";
        } catch (Exception e) {
            e.printStackTrace();
            return e.getMessage();
        }
    }
    @ApiOperation(value = "刷新单个股票的价格到今天", notes = "刷新单个股票的价格到今天,tushare接口到stock_price_dail")
    @ResponseBody
    @PostMapping(value = "/refreshDailyPriceToMax")
    public String refreshDailyPriceToMax(@RequestParam String code) {
        try {
            stockDailyPriceServices.refreshDailyPriceToMax(code);
            return "success";
        } catch (Exception e) {
            e.printStackTrace();
            return e.getMessage();
        }
    }
    @ApiOperation(value = "刷新指数", notes = "刷新指数,雪球网接口到stock_price_daily，需要登录获取cookie，配置常量")
    @ResponseBody
    @PostMapping(value = "/refresh_price_indexsh")
    public String initIndexesPrice() {
        try {
            stockDailyIndexesService.refreshDailyIndexes();
            return "success";
        } catch (Exception e) {
            e.printStackTrace();
            return e.getMessage();
        }
    }

    @ApiOperation(value = "刷新dy值", notes = "刷新5年平均dy值")
    @ResponseBody
    @GetMapping(value = "/mapReduce/dy/{start}/{end}/{key}")
    public String fiveYearsDy(@PathVariable("start") Integer start,@PathVariable("end") Integer end ,@PathVariable("key")String key) {
        service.calculateFiveYearsDy(start,end,key);
        return String.format("success:%s - %s - %s", start, end,key);
    }

    @ApiOperation(value = "刷新roe值", notes = "刷新5年平均roe值")
    @ResponseBody
    @GetMapping(value = "/mapReduce/roe/{start}/{end}/{key}")
    public String fiveYearsRoe(@PathVariable("start") int start,@PathVariable("end") int end ,@PathVariable("key")String key) {
        service.calculateFiveYearsRoe(start,end,key);
        return String.format("success:%s - %s - %s", start, end,key);
    }

    @Autowired
    private MyMongoTemplate myMongoTemplate;

    @GetMapping(value = "/csindex")
    public String crawCsindex()throws Exception{
        CsindexIndustrySpider csindexIndustrySpider = new CsindexIndustrySpider();
        FutureTask<List<CsIndexIndustry>> task = new FutureTask<>(csindexIndustrySpider);
        new Thread(task).start();
        List<CsIndexIndustry> list=task.get();
        myMongoTemplate.dropCollection(CsIndexIndustry.class);
        BulkWriteResult bulkWriteResult= myMongoTemplate.save(list,CsIndexIndustry.class);
        return JSON.toJSONString(bulkWriteResult);
    }

    @ApiOperation(value = "刷新行业数据", notes = "刷新行业数据")
    @ResponseBody
    @PostMapping(value = "/refreshIndustry")
    public String refreshIndustry(@RequestParam String cookieStr) {
        try {
            boardIndustryService.refreshData(cookieStr);
            return "success";
        } catch (Exception e) {
            e.printStackTrace();
            return e.getMessage();
        }
    }

    @ApiOperation(value = "刷新今天股价数据stock_price_today", notes = "刷新今天股价数据")
    @ResponseBody
    @PostMapping(value = "/refreshStockPriceToday")
    public String refreshStockPriceToday(@RequestParam String cookieStr) {
        try {
            stockPriceTodayService.refreshData(cookieStr);
            return "success";
        } catch (Exception e) {
            e.printStackTrace();
            return e.getMessage();
        }
    }
}
